/*
 * Copyright 2010 - 2011, The PLDesktop Development Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#include "thread.h"

#include <machine/pc/process/process.h>
#include <machine/pc/process/processinstancemanager.h>

PLThread::PLThread(PLProcess *_process) : m_process(_process), m_parentThread(0), m_id(-1) {
}

PLThread::PLThread(PLProcess *_process, PLThread *_parent) : m_process(_process), m_parentThread(_parent), m_id(-1) {
}

void PLThread::init() {
	clearState();
}

void PLThread::clear() {
	clearState();
}

void PLThread::clearState() {
	m_state = static_cast<InterruptState *>((void*)(m_stack + STACKSIZE - sizeof(m_state)));
	m_state->m_Ds = 0;
	m_state->m_Edi = 0;
	m_state->m_Esi = 0;
	m_state->m_Ebp = 0;
	m_state->m_Res = 0;
	m_state->m_Ebx = 0;
	m_state->m_Edx = 0;
	m_state->m_Ecx = 0;
	m_state->m_Eax = 0;
	m_state->m_IntNumber = 0;
	m_state->m_Errorcode = 0;
	m_state->m_Eip = 0; // todo entry
	m_state->m_Cs = 0x08;
	m_state->m_Eflags = 0x202;
	m_state->m_Esp = (uint32_t)(m_stack + 4096);
	m_state->m_Ss = 0;
}

PLThread *PLThread::createThread() {
	return PLProcessInstanceManager::instance().newThread(m_process, this);
}
